// pages/live-center-merchant/live-center-merchant.js
const { globalData } = getApp();
import {
    liveStatus
  } from '../../utils/const.js'
  
  // 直播状态
  const liveBtn = {
    '101': '观看直播',
    '102': '查看预告',
    '103': '查看回放'
  }
  
  const iconMap = {
    '101': '../../assets/images/icon/playing.png',
    '102': '../../assets/images/icon/no-play.png',
    '103': '../../assets/images/icon/play-back.png'
  }
Page({
    /**
     * 页面的初始数据
     */
    data: {
        active: "0",
        params:{
            type: 'video_type_xsp',
            merchant_id: "2312"
        },
        vrParams:{
            type: 'video_type_vr' 
        },
        merchant_id: "",
         // 直播列表
        list: [],
        // 回放列表
        pageList: [],
        pageViedoList:[],
        pageVrViedoList:[],
        page: 1,
        pageSize: 10,
        totalPages: 0
    },
    handleTabChange(event) {
        const { value } = event.currentTarget.dataset;
        this.setData({
          active: value
        });
      },
      itemClickHandle(event) {
        const {
          index
        } = event.currentTarget.dataset
        let {
          roomid
        } = this.data.list[index]
        wx.navigateTo({
          url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomid}`
        })
      },
      backClickHandle(event) {
        const {page, index} = event.currentTarget.dataset
        let {roomid} = this.data.pageList[page][index]
        wx.navigateTo({
          url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomid}`
        })
      },
      requestList() {
        wx.ajax
          .post('queryDoingLiveList.action', {merchant_id: this.merchant_id})
          .then(({
            time,
            result
          }) => {
            let date = new Date(time.replaceAll('-', '/'))
    
            result.forEach(item => {
              item.statusName = liveStatus[item.live_status]
              item.btnText = liveBtn[item.live_status]
              item.iconUrl = iconMap[item.live_status]
              if (item.live_status == '102') {
                item.countDown = this.computedTime((item.start_time * 1000) - date.getTime())
              }
            })
    
            wx.stopPullDownRefresh()
            this.setData({
              list: result
            });
          })
          .catch(() => {
            wx.stopPullDownRefresh()
          });
      },
      computedTime(timeDuration) {
        let timeArr = [0, 0, 0]
        if (timeDuration > 0) {
          let miniteNum = 60 * 1000,
            hourNum = 60 * miniteNum,
            dayNum = 24 * hourNum;
          let temp = Math.floor(timeDuration / dayNum),
            str = '';
          if (temp > 0) {
            timeArr[0] = temp
          }
          timeDuration = timeDuration % dayNum;
          temp = Math.floor(timeDuration / hourNum)
          if (temp > 0) {
            timeArr[1] = temp
          }
          timeDuration = timeDuration % hourNum;
          temp = Math.floor(timeDuration / miniteNum)
          if (temp > 0) {
            timeArr[2] = temp
          }
        }
        return `${timeArr[0]}天${timeArr[1]}时${timeArr[2]}分`
      },
      // 请求回放列表
      requestBackList(page) {
        if (page > 1 && page > this.data.totalPages) {
          return
        }
    
        wx.ajax
          .post('queryReplayList.action', {
            merchant_id: this.merchant_id,
            page,
            pageSize: this.data.pageSize
          })
          .then(({
            result
          }) => {
            let data = result || {};
            data.pageList = data.pageList || []
            data.pageList.forEach(item => {
              item.statusName = liveStatus[item.live_status]
              item.btnText = liveBtn[item.live_status]
              item.iconUrl = iconMap[item.live_status]
            })
    
            this.setData({
              page: data.page,
              totalPages: data.totalPages,
              [`pageList[${data.page - 1}]`]: data.pageList
            });
          })
      },
      requestRefresh() {
        this.requestList()
        this.requestBackList()
      },

      pageDataHandle(event) {
        const {page, list = []} = event.detail;
        let setData = {}
    
        list.forEach(item => {
          item.head_img = globalData.imageUrl + item.head_img
          item.video_url = globalData.imageUrl + item.video_url
        })
    
        if (page == 1) {
          setData.pageViedoList = [list]
        } else {
          setData[`pageViedoList[${page}]`] = list
        }
    
        this.setData(setData)
      },
      pageVrDataHandle(event) {
        const {page, list = []} = event.detail;
        let setData = {}
    
        list.forEach(item => {
          item.head_img = globalData.imageUrl + item.head_img
          item.video_url = globalData.imageUrl + item.video_url
        })
    
        if (page == 1) {
          setData.pageVrViedoList = [list]
        } else {
          setData[`pageVrViedoList[${page}]`] = list
        }
    
        this.setData(setData)
      },
      itemVideoClickHandle(event) {
        const {page, index} = event.currentTarget.dataset
        let data = this.data.pageViedoList[page][index]
        data = encodeURIComponent(JSON.stringify(data))
        wx.navigateTo({
          url: `/pages/video-play/video-play?video=${data}`,
        })
      },
      itemVrVideoClickHandle(event) {
        const {page, index} = event.currentTarget.dataset
        let data = this.data.pageVrViedoList[page][index]
        
        //data = encodeURIComponent(JSON.stringify(data))
        wx.navigateTo({
         url: "/vr/pages/vr-info/vr-info?infoId=" + data.id+"&findVrType=3&oss=oss",
        })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.merchant_id = options.merchant_id,
        this.setData({
            params: {
                type: 'video_type_xsp',
                merchant_id: options.merchant_id
            },
            vrParams:{
                type: 'video_type_vr',
                merchant_id: options.merchant_id
            }
          });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.requestRefresh()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.requestRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.requestBackList(this.data.page + 1)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        return {
            path: `/pages/live-center/live-center`
          }
    }
})